﻿using Newtonsoft.Json.Linq;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace miniQQ_CSharp_SDK
{
    internal class InviteSystem
    {
        JObject jo = new JObject();
        public Root root = new Root();
        string path = @"邀请系统.json";
        public InviteSystem()
        {
            Init();
        }
        public void Init()
        {
            if (JsonReady())
            {
                if(!JsonLinqExt.TryGetValue(jo, "root", out root))
                {
                    root = new Root();
                }
            }
        }
        public bool JsonReady()
        {
            if (File.Exists(path))
            {
                using (StreamReader sr = new StreamReader(path))
                {
                    string json = sr.ReadToEnd();
                    try
                    {
                        this.jo = JObject.Parse(json);
                        return true;
                    }
                    catch
                    {
                        return false;
                    }

                }
            }
            else return false;
            
        }
        public void WriteJson()
        {
            using(StreamWriter sw = new StreamWriter(path))
            {
                sw.WriteLine(ParseToJObject().ToString());
            }
        }

        public JObject ParseToJObject()
        {
            JObject jo = new JObject();
            JsonLinqExt.Add(jo, "root", this.root);
            return jo;
        }
        public void AddNewUser(string UserQQ,string QQGroup)
        {
            User user = new User(UserQQ);
            if(GetQQGroup(QQGroup) != null)
            {
                GetQQGroup(QQGroup).Users.Add(user);
            }
            else
            {
                AddNewQQGroup(QQGroup);
                GetQQGroup(QQGroup).Users.Add(user);
            }
        }

        public void AddNewQQGroup(string GroupNum)
        {
            QQGroup qQGroup = new QQGroup(GroupNum);
            if(GetQQGroup(GroupNum) == null)
            {
                root.QQGroups.Add(qQGroup);
            }
            
        }
        public User GetUser(string UserQQ,string QQGroup)
        {
            if(GetQQGroup(QQGroup) != null)
            {
                foreach (User user in GetQQGroup(QQGroup).Users)
                {
                    if (user.UserQQ == UserQQ)
                    {
                        return user;
                    }
                }
            }
            User _user = new User(UserQQ);
            GetQQGroup(QQGroup).Users.Add(_user);
            return _user;
        }
        public QQGroup GetQQGroup(string GroupNum)
        {
            foreach(var qQGroup in root.QQGroups)
            {
                if(qQGroup.GroupNum == GroupNum)
                {
                    return qQGroup;
                }
            }
            QQGroup _qQGroup = new QQGroup(GroupNum);
            root.QQGroups.Add(_qQGroup);
            return _qQGroup;
        }

        public void Leave(string UserQQ, string GroupNum)
        {
            if (GetQQGroup(GroupNum) != null && GetUser(UserQQ, GroupNum) != null)
            {
                GetQQGroup(GroupNum).Leave(GetUser(UserQQ, GroupNum));
            }
        }

    }
}
